package com.xiaohuihui.springbootsecurity.handler;

import com.alibaba.fastjson.JSON;
import com.xiaohuihui.springbootsecurity.domain.ApiResult;
import com.xiaohuihui.springbootsecurity.utils.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        ApiResult apiResult =ApiResult.failed("认证失败");
        String json = JSON.toJSONString(apiResult);
        //处理异常
        WebUtils.renderString(response,json);
    }
}